package com.example.hyw.szysteam.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hyw.szysteam.POJO.Practice;
import com.example.hyw.szysteam.POJO.Review;
import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.presenter.PracticePresenter;
import com.example.hyw.szysteam.utils.ActivityManager;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUIFloatLayout;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/10.
 */

public class PracticeActivity extends AppCompatActivity {
    TextView practiceQuestion;
    EditText practiceAnswer;
    LinearLayout practiceUp;
    LinearLayout practiceDown;
    QMUIFloatLayout floatLayout;
    QMUITopBar mTopBar;
    int current=0;
    int testid=-1;
    ArrayList<Practice> practices=new ArrayList<>();
    ArrayList<String> answer=new ArrayList<>();
    PracticePresenter practicePresenter=new PracticePresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        initView();
        initTopbar();
        ActivityManager.addDestoryActivity(PracticeActivity.this,"practice");
        Bundle bundle=getIntent().getExtras();
        if (bundle.getInt("type")==1) {
            int number = bundle.getInt("number");
            int operator = bundle.getInt("operator");
            int range = bundle.getInt("range");
            practicePresenter.initPractice(number,operator,range);
        }else if (bundle.getInt("type")==2){
            int testid=bundle.getInt("testid");
            practicePresenter.getScore(UserInfo.getEmail(),testid);
            this.testid=testid;
            practicePresenter.getPractice(testid);
        }else if(bundle.getInt("type")==3){
            ArrayList<Review> reviews=bundle.getParcelableArrayList("list");
            ArrayList<Practice> practices=new ArrayList<>();
            for (int i=0;i<reviews.size();i++){
                Practice p=new Practice();
                p.setQuestion(reviews.get(i).getQuestion());
                p.setAnswer(reviews.get(i).getAnswer());
                p.setId(reviews.get(i).getId());
                practices.add(p);
            }
            initPractice(practices);
        }
    }

    private void initView(){
        practiceQuestion=findViewById(R.id.practice_question);
        practiceAnswer=findViewById(R.id.practice_answer);
        practiceUp=findViewById(R.id.practice_up);
        practiceDown=findViewById(R.id.practice_down);
        floatLayout=findViewById(R.id.practice_number);
        practiceUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(current>0) {
                    if (practiceAnswer.getText().toString().equals("")) {
                        floatLayout.getChildAt(current).setBackgroundResource(R.color.mygray1);
                    } else {
                        floatLayout.getChildAt(current).setBackgroundResource(R.color.mygreen);
                        answer.set(current, practiceAnswer.getText().toString());
                    }
                    current--;
                    floatLayout.getChildAt(current).setBackgroundResource(R.color.myblue);
                    practiceQuestion.setText(practices.get(current).getQuestion());
                    practiceAnswer.setText(answer.get(current));
                }else {
                    Toast.makeText(getApplicationContext(),"这是第一题！",Toast.LENGTH_SHORT).show();
                }
            }
        });
        practiceDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(current<answer.size()-1){
                    if (practiceAnswer.getText().toString().equals("")) {
                        floatLayout.getChildAt(current).setBackgroundResource(R.color.mygray1);
                    } else {
                        floatLayout.getChildAt(current).setBackgroundResource(R.color.mygreen);
                        answer.set(current, practiceAnswer.getText().toString());
                    }
                    current++;
                    floatLayout.getChildAt(current).setBackgroundResource(R.color.myblue);
                    practiceQuestion.setText(practices.get(current).getQuestion());
                    practiceAnswer.setText(answer.get(current));
                }else {
                    Toast.makeText(getApplicationContext(),"这是最后一题！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void initPractice(ArrayList<Practice> practices){
        this.practices=practices;
        for (int i=0;i<practices.size();i++){
            answer.add("");
            addItemToFloatLayout(floatLayout,R.color.mygray1);
        }
        floatLayout.getChildAt(0).setBackgroundResource(R.color.myblue);
        practiceQuestion.setText(practices.get(0).getQuestion());
    }

    private void addItemToFloatLayout(final QMUIFloatLayout floatLayout, int color){
        final int currentChildCount=floatLayout.getChildCount()+1;
        final TextView textView=new TextView(this);
        current=0;
        int textViewPadding= QMUIDisplayHelper.dp2px(this,4);
        textView.setPadding(textViewPadding,textViewPadding,textViewPadding,textViewPadding);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        textView.setText(String.valueOf(currentChildCount));
        textView.setBackgroundResource(color);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(practiceAnswer.getText().toString().equals("")){
                    floatLayout.getChildAt(current).setBackgroundResource(R.color.mygray1);
                }else {
                    floatLayout.getChildAt(current).setBackgroundResource(R.color.mygreen);
                    answer.set(current,practiceAnswer.getText().toString());
                }
                textView.setBackgroundResource(R.color.myblue);
                practiceQuestion.setText(practices.get(currentChildCount-1).getQuestion());
                practiceAnswer.setText(answer.get(currentChildCount-1));
                current=currentChildCount-1;
            }
        });
        int textViewSize=QMUIDisplayHelper.dpToPx(60);
        ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(textViewSize,textViewSize);
        floatLayout.addView(textView,layoutParams);
    }

    private void initTopbar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QMUIDialog.MessageDialogBuilder(PracticeActivity.this)
                        .setTitle("退出")
                        .setMessage("确定要退出吗？")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                                ActivityManager.destoryActivity("practice");
                            }
                        })
                        .show();
            }
        });
        mTopBar.setTitle("测试").setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addRightTextButton("提交",R.id.right).setTextColor(getResources().getColor(R.color.myWhite));
        View rightButton=findViewById(R.id.right);
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QMUIDialog.MessageDialogBuilder(PracticeActivity.this)
                        .setTitle("提交")
                        .setMessage("确定要提交吗？")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                answer.set(current,practiceAnswer.getText().toString());
                                dialog.dismiss();
                                practicePresenter.correct(practices,answer);
                            }
                        })
                        .show();
            }
        });
    }

    public void showScore(int score, ArrayList<Review> reviews){
        if(testid>-1){
            practicePresenter.setScore(UserInfo.getEmail(),testid,score);
        }
        Bundle bundle=new Bundle();
        bundle.putParcelableArrayList("reviews",reviews);
        bundle.putInt("score",score);
        bundle.putInt("testid",testid);
        Intent intent=new Intent(PracticeActivity.this,ScoreActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

    public void completed(int score){
        Bundle bundle=new Bundle();
        bundle.putInt("score",score);
        Intent intent=new Intent(PracticeActivity.this,TestFinishActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }
}
